// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/common/file/file_systems.h>

namespace kumo::pollux::filesystems {

struct HdfsServiceEndpoint {
  HdfsServiceEndpoint(
      const std::string& hdfsHost,
      const std::string& hdfsPort,
      bool isViewfs = false)
      : host(hdfsHost), port(hdfsPort), isViewfs(isViewfs) {}

  /// In HDFS HA mode, the identity is a nameservice ID with no port, e.g.,
  /// the identity is nameservice_id for
  /// hdfs://nameservice_id/file/path/in/hdfs. Otherwise, a port must be
  /// contained, e.g., the identity is hdfs_namenode:9000 for
  /// hdfs://hdfs_namenode:9000/file/path/in/hdfs.
  std::string identity() const {
    return host + (port.empty() ? "" : ":" + port);
  }

  const std::string host;
  const std::string port;
  bool isViewfs;
};

/**
 * You can configure hdfs settings (timeouts etc) using configure file
 * which is given by environment parameter LIBHDFS3_CONF
 * or "hdfs-client.xml" in working directory.
 *
 * Internally you can use hdfsBuilderConfSetStr to configure the client
 */
class HdfsFileSystem : public FileSystem {
 public:
  explicit HdfsFileSystem(
      const std::shared_ptr<const config::ConfigBase>& config,
      const HdfsServiceEndpoint& endpoint);

  std::string name() const override;

  std::unique_ptr<ReadFile> openFileForRead(
      std::string_view path,
      const FileOptions& options = {}) override;

  std::unique_ptr<WriteFile> openFileForWrite(
      std::string_view path,
      const FileOptions& options = {}) override;

  void remove(std::string_view path) override;

  virtual void rename(
      std::string_view path,
      std::string_view newPath,
      bool overWrite = false) override {
    POLLUX_UNSUPPORTED("rename for HDFs not implemented");
  }

  bool exists(std::string_view path) override {
    POLLUX_UNSUPPORTED("exists for HDFS not implemented");
  }

  virtual std::vector<std::string> list(std::string_view path) override {
    POLLUX_UNSUPPORTED("list for HDFS not implemented");
  }

  void mkdir(std::string_view path, const DirectoryOptions& options = {})
      override {
    POLLUX_UNSUPPORTED("mkdir for HDFS not implemented");
  }

  void rmdir(std::string_view path) override {
    POLLUX_UNSUPPORTED("rmdir for HDFS not implemented");
  }

  static bool isHdfsFile(std::string_view filename);

  /// The given filePath is used to infer hdfs endpoint. If hdfs identity is
  /// missing from filePath, the configured "hive.hdfs.host" & "hive.hdfs.port"
  /// will be used.
  static HdfsServiceEndpoint getServiceEndpoint(
      const std::string_view filePath,
      const config::ConfigBase* config);

  static std::string_view kScheme;

  static std::string_view kViewfsScheme;

 protected:
  class Impl;
  std::shared_ptr<Impl> impl_;
};

} // namespace kumo::pollux::filesystems
